package com.netease.nie.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.base.exceptions.UnexpectedException;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.JsonUtils;
import com.netease.nie.constants.CommonConstants;
import com.netease.nie.dao.NavDao;
import com.netease.nie.dto.ClassifiedNavDto;
import com.netease.nie.model.ClassifiedNav;
import com.netease.nie.model.NavSetting;

/**
 * 
 * @author KingsHunter
 * @createDate June 29th,2016
 *
 */
@Service
public class NavServiceImpl implements NavService {
	
	private static final String NAV_SETTING_KEY = "NAV_SETTING";
	
	private static final String CLASSIFIED_NAV_KEY = "Classified_Nav_List";
	
	private static final String VISIBLE = "visible";
	
	private static final String LINE_DISPLAY_COUNT = "lineDisplayCount";

	@Autowired
	private NavDao navDao;
	
	@Autowired
	private Redis redis;
	
	private Map<String, Integer> queryNavSetting() {
		List<String> strList = redis.hmget(NAV_SETTING_KEY, VISIBLE, LINE_DISPLAY_COUNT);
		Map<String, Integer> map = new HashMap<>();
		if (strList != null && !strList.isEmpty()) {
			if (StringUtils.isNotBlank(strList.get(0)))
				map.put(VISIBLE, Integer.valueOf(strList.get(0)));

			if (StringUtils.isNotBlank(strList.get(1)))
				map.put(LINE_DISPLAY_COUNT, Integer.valueOf(strList.get(1)));
			return map;
		} else {
			NavSetting navSetting = navDao.queryNavSetting();
			if (navSetting != null) {
				int visible = navSetting.getIsVisible();
				int lineDisplayCount = navSetting.getLineDisplayCount();
				map.put(VISIBLE, visible);
				map.put(LINE_DISPLAY_COUNT, lineDisplayCount);
				Map<String, String> fieldValues = new HashMap<String, String>();
				fieldValues.put(VISIBLE, String.valueOf(visible));
				fieldValues.put(LINE_DISPLAY_COUNT, String.valueOf(lineDisplayCount));
				redis.hmset(NAV_SETTING_KEY, fieldValues);
				return map;
			}
		}
		//待添加日志记录异常
		throw new UnexpectedException("关键数据缺失");
	}

	private List<ClassifiedNav> queryAllClassifiedNav() {
		Map<String, Integer> navSettingMap = queryNavSetting();
		int visible = navSettingMap.get(VISIBLE);
		//不展示导航
		if (visible == 0)
			return Collections.emptyList();
		int lineDisplayCount = navSettingMap.get(LINE_DISPLAY_COUNT);
		int start = 0;
		int end = lineDisplayCount - 1;
		List<String> strList = redis.lrange(CLASSIFIED_NAV_KEY, start, end);
		String text = null;
		ClassifiedNav classifiedNav = null;
		List<ClassifiedNav> classifiedNavList = null;
		if (strList != null && !strList.isEmpty()) {
			classifiedNavList = new ArrayList<>();
			int strListSize = strList.size();
			for (int i = 0; i < strListSize; i++) {
				text = strList.get(i);
				if (StringUtils.isNotBlank(text)) {
					classifiedNav = JsonUtils.getClazz(text, ClassifiedNav.class);
					classifiedNavList.add(classifiedNav);
				}
			}
		} else {
			classifiedNavList = navDao.listClassifiedNavByNavSetting(lineDisplayCount);
			if (classifiedNavList != null && !classifiedNavList.isEmpty()) {
				int classifiedNavListSize = classifiedNavList.size();
				String[] classifiedNavArr = {};
				for (int i = 0; i < classifiedNavListSize; i++) {
					classifiedNav = classifiedNavList.get(i);
					classifiedNavArr = (String[]) ArrayUtils.add(classifiedNavArr, JsonUtils.obj2String(classifiedNav));
				}
				
				if (classifiedNavArr.length > 0)
					redis.rpush(CLASSIFIED_NAV_KEY, classifiedNavArr);
			}
		}
		
		return classifiedNavList;
	}

	public List<ClassifiedNavDto> queryAllClassifiedNavDto() {
		List<ClassifiedNav> classifiedNavList = queryAllClassifiedNav();
		if (classifiedNavList != null && !classifiedNavList.isEmpty()) {
			List<ClassifiedNavDto> classifiedNavDtoList = new ArrayList<>();
			ClassifiedNavDto classifiedNavDto = null;
			int classifiedNavListSize = classifiedNavList.size();
			ClassifiedNav classifiedNav = null;
			for (int i = 0; i < classifiedNavListSize; i++) {
				classifiedNavDto = new ClassifiedNavDto();
				classifiedNav = classifiedNavList.get(i);
				CopyUtils.copy(classifiedNavDto, classifiedNav);
				//classifiedNavDto.setIconUrl(CommonConstants.PIC_PREFIX + classifiedNav.getIconUrl());
				classifiedNavDtoList.add(classifiedNavDto);
			}
			return classifiedNavDtoList;
		}
		return Collections.emptyList();
	}
	
}
